home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group97a.txt
/
000126_icon-group-sender _Sun May 18 12:31:04 1997.msg
< prev
next >
Wrap
Internet Message Format
|
2000-09-20
|
3KB
Received: from kingfisher.CS.Arizona.EDU by cheltenham.cs.arizona.edu; Mon, 19 May 1997 09:55:21 MST
Received: by kingfisher.CS.Arizona.EDU; (5.65v3.2/1.1.8.2/08Nov94-0446PM)
id AA07392; Mon, 19 May 1997 09:55:21 -0700
Date: Sun, 18 May 1997 12:31:04 -0400
Message-Id: <199705181631.MAA19857@axp.cmpu.net>
Mime-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
From: gep2@computek.net
Subject: Problem with Program
To: icon-group@cs.arizona.edu
X-Mailer: SPRY Mail Version: 04.00.06.17
Errors-To: icon-group-errors@cs.arizona.edu
Status: RO
Content-Length: 2453
>Could someone tell me why the program below isn't working? It's late and
I must be overlooking something. Given input like the following
>This is a sample line 1{S {S
This is a sample line 2{A {A
This is a sample line 3{O {O
>it should be produce the following output
S: 2
A: 2
O: 2
>Thanks in advance.
----------------
>procedure main()
chars := &letters ++ &digits ++ '{.*[]?' #valid characters
noS := 0
noA := 0
noO := 0
while line := read() do #read in line
{
line ? #textscan line
{
while tab(upto(chars)) do #look for valid character
{
word := tab(many(chars)) #word = valid character through
next invalid character
word ? #textscan word
{
while tab(upto("{") +1) do #move just past bracket (will
fail if there is none)
{
wordfrag := move(1) #wordfrag = one character past bracket
case wordfrag of #if wordfrag is
{
"S" : noS +:= 1 #S, then increment noS by 1
"A" : noA +:= 1 #A, then increment noA by 1
"O" : noO +:= 1 #O, then increment noO by 1
}
}
}
}
}
}
write( "S: " || noS || \n || "A: " || noA || \n || "O: " || noO )
end
To start with, you're writing this in what seems like a terribly convoluted way,
and not letting yourself take advantage of the kinds of things that set
Icon/S*BOL apart from other languages (notably, tables). Secondly, I'd look for
the "{" character followed by something, and then use that something to
increment a counter in a table. This prevents you having to hardwire in just
three key letters.
For instance, if I were going to write the same program in S*BOL, it might look
like:
t = table()
pat = fence breakx("{") "{" notany(" ") . key
readrec line = input :f(donerd)
countlp line pat = :f(readrec)
t[key] = t[key] + 1 :(countlp)
donerd a = convert(t,"ARRAY") :f(end)
outlp output = a[(i = i + 1), 1] " " a[i,2] :s(outlp)
end
Gordon Peterson
http://www.computek.net/public/gep2/
Support the Anti-SPAM Amendment! Join at http://www.cauce.org/